import { _decorator, Component, Animation, AnimationClip, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enum';
import State from '../../common/State';
import { getInitParamsNumber, getInitParamsTrigger, StateMachine } from '../../common/StateMachine';
import { IdleSubStateMachine } from './IdleSubStateMachine';
import { EntiyManager } from '../../common/EnityManager';
import { DeadSubStateMachine } from './DeadSubStateMachine';
const { ccclass, property } = _decorator;


@ccclass('IronSkeletonStateMachine')
export class IronSkeletonStateMachine extends StateMachine {

  async init(){
    this.animationComponent = this.addComponent(Animation)
    this.initParams()
    this.initStateMachines()
    this.initAnimationEvent()
    await Promise.all(this.waitingList)
  }


  initParams(){
    this.params[PARAMS_NAME_ENUM.DIRECTION] = getInitParamsNumber()
    this.params[PARAMS_NAME_ENUM.IDLE] = getInitParamsTrigger()
    this.params[PARAMS_NAME_ENUM.DEAD] = getInitParamsTrigger()

  }

  initStateMachines(){
    this.stateMachine[PARAMS_NAME_ENUM.IDLE] = new IdleSubStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.DEAD] = new DeadSubStateMachine(this)
  }

  initAnimationEvent(){
    this.animationComponent.on(Animation.EventType.FINISHED, () => {

      const name = this.animationComponent.defaultClip.name
      const whiteList = ['attack']
      if(whiteList.some(v=> name.includes(v))){
        this.node.getComponent(EntiyManager).state = ENTITY_STATE_ENUM.IDLE
        // this.setParam(PARAMS_NAME_ENUM.IDLE, true)
      }

    })
  }


  run(){
    switch (this.currentState){
      case this.stateMachine[PARAMS_NAME_ENUM.IDLE]:
      case this.stateMachine[PARAMS_NAME_ENUM.DEAD]:
        if (this.params[PARAMS_NAME_ENUM.IDLE].value){
          this.currentState = this.stateMachine[PARAMS_NAME_ENUM.IDLE]
        } else if (this.params[PARAMS_NAME_ENUM.DEAD].value){
          this.currentState = this.stateMachine[PARAMS_NAME_ENUM.DEAD]
        } else{
          this.currentState = this.currentState
        }
        break;
      default:
        this.currentState = this.stateMachine[PARAMS_NAME_ENUM.IDLE]
    }
  }



}
